/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sv.com.intesal.compra.managed;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import sv.com.intesal.clases.LimpiarManaged;
import sv.com.intesal.clases.Mensaje;
import sv.com.intesal.compra.bean.Pais;
import sv.com.intesal.compra.controlador.PaisControlador;
import sv.com.intesal.seguridad.managed.SessionManaged;

/**
 *
 * @author JM
 */
@ManagedBean(name = "paisManaged")
@ViewScoped
public class PaisManaged implements Serializable {

    private Pais selectedPais;
    private List<Pais> paises;
    private Mensaje m;
    private SessionManaged sm;

    public PaisManaged() throws SQLException {
        selectedPais = new Pais();
        m = new Mensaje();
        sm = new SessionManaged();
    }

    public void fillGrid() throws SQLException {
        try {
            if (sm.validar_rol_usuario("wfrmPais", sm.buscar) == true) {
                if (paises == null) {
                    PaisControlador pc = new PaisControlador();
                    paises = pc.getAllPaises();
                }
            }
        } catch (Exception ex) {
            m.message(m.getIerror());
            ex.printStackTrace();
        }
    }

    public void accionPais() {

        try {
            if (this.validar() == true) {
                if (this.selectedPais.getId_p() == 0) {
                    if (sm.validar_rol_usuario("wfrmPais", sm.agregar) == true) {
                        PaisControlador pc = new PaisControlador();
                        pc.mantenimientoPais(this.getSelectedPais());
                        LimpiarManaged.limpiar();
                    }
                } else {
                    if (sm.validar_rol_usuario("wfrmPais", sm.modificar) == true) {
                        PaisControlador pc = new PaisControlador();
                        pc.mantenimientoPais(this.getSelectedPais());
                        LimpiarManaged.limpiar();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            m.messageError(m.getIerror());
        }
    }

    private Boolean validar() throws Exception {

        try {
            if (this.getSelectedPais().getNombre_p().equals("")) {
                m.messageAdvertencia(m.campoError("NOMBRE"));
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
            throw new Exception(e);
        }

    }

    /*SETTERS AND GETTERS*/
    public Pais getSelectedPais() {
        if (this.selectedPais == null) {
            this.selectedPais = new Pais();
        }
        return selectedPais;
    }

    public void setSelectedPais(Pais selectedPais) {
        this.selectedPais = selectedPais;
    }

    public List<Pais> getPaises() throws SQLException {
        return paises;
    }

    public void setPaises(List<Pais> paises) {
        this.paises = paises;
    }
}
